/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "hhdOutputStream.h"
#include "hhdPrintStream.h"

#include "hhdString.h"
#include "hhdFloat.h"
#include "hhdDouble.h"

#include <cstdio>
// gets rid of the sprintf warnings, there won't be any problems with that here
#pragma warning( push )
#pragma warning(disable : 4996)

namespace hhd
{
	PrintStream::PrintStream(OutputStream& in_output_stream)
	{
		_output_stream = &in_output_stream;
	}

	void PrintStream::flush()
	{
		_output_stream->flush();
	}

	void PrintStream::write(char8_t in_char)
	{
		_output_stream->write(in_char);
	}

	void PrintStream::write(const char8_t* in_bytes, size_t in_count)
	{
		_output_stream->write(in_bytes, in_count);
	}

	PrintStream& PrintStream::operator<<(PrintStream& (*pf)(PrintStream&))
	{
		return pf(*this);
	}

	PrintStream& PrintStream::operator<<(const char8_t* in_string)
	{
 		_output_stream->write(in_string);
		return *this;
	}

	// integer types
	PrintStream& PrintStream::operator<<(char8_t in_char)
	{
		_output_stream->write(in_char);
		return *this;
	}

	PrintStream& PrintStream::operator<<(uchar8_t in_uchar)
	{
		_output_stream->write((char8_t)in_uchar);
		return *this;
	}

	PrintStream& PrintStream::operator<<(bool in_bool)
	{
		if(in_bool)
			_output_stream->write("True", 4);
		else
			_output_stream->write("False", 5);
		return *this;
	}

	PrintStream& PrintStream::operator<<(int32_t in_int)
	{
		char buffer[12];
		_output_stream->write(buffer, sprintf(buffer, "%d", in_int));
		return *this;
	}

	PrintStream& PrintStream::operator<<(uint32_t in_uint)
	{
		char buffer[11];
		_output_stream->write(buffer, sprintf(buffer, "%u", in_uint));
		return *this;
	}

	PrintStream& PrintStream::operator<<(int64_t in_long_long)
	{
		char buffer[19];
		_output_stream->write(buffer, sprintf(buffer, "%" HHD_D64, in_long_long));
		return *this;
	}

	PrintStream& PrintStream::operator<<(uint64_t in_ulong_long)
	{
		char buffer[19];
		_output_stream->write(buffer, sprintf(buffer, "%" HHD_U64, in_ulong_long));
		return *this;
	}

	// floating point
	PrintStream& PrintStream::operator<<(float in_float)
	{
		if(in_float != in_float)
			_output_stream->write("NaN", 3);
		else if(in_float == Float::Infinity)
			_output_stream->write("Infinity", 8);
		else if(in_float == Float::NegativeInfinity)
			_output_stream->write("-Infinity", 9);
		else
		{
			char buffer[15];
			_output_stream->write(buffer, sprintf(buffer, "%.6g", in_float));
		}
		return *this;
	}

	PrintStream& PrintStream::operator<<(double in_double)
	{
		if(in_double != in_double)
			_output_stream->write("NaN", 3);
		else if(in_double == Double::Infinity)
			_output_stream->write("Infinity", 8);
		else if(in_double == Double::NegativeInfinity)
			_output_stream->write("-Infinity", 9);
		else
		{
			char buffer[26];
			_output_stream->write(buffer, sprintf(buffer, "%.15g", in_double));
		}
		return *this;
	}

	// other
	PrintStream& PrintStream::operator<<(const void* in_pointer)
	{
#ifdef _WIN64
		char buffer[19];
		_output_stream->write(buffer, sprintf(buffer, "0x%016lx", (long long)in_pointer));
		return *this;
#else
		char buffer[11];
		_output_stream->write(buffer, sprintf(buffer, "0x%08lx", (unsigned long)in_pointer));
		return *this;
#endif
	}

	PrintStream& PrintStream::operator<<(const String& in_string)
	{
		_output_stream->write(in_string, in_string.length());
		return *this;
	}

	PrintStream& endl(PrintStream& in_stream)
	{
		in_stream.write('\n');
		return in_stream;
	}
		
	PrintStream& flush(PrintStream& in_stream)
	{
		in_stream.flush();
		return in_stream;
	}
}

// reenable the 4996 warning after this
#pragma warning( pop )